<?php
defined( 'IN_DICE' ) or die( 'Access Error!' );

include_once( DIR_MDL . 'platform/platform.php' );
include_once( DIR_MDL . 'platform/gamebookapi.php' );
include_once( DIR_MDL . 'platform/class.gamebook.php' );
class gamebook implements platform{
  
  /**
   * @var gamebook
   */
  public $op;
  public $pfid;
  public $apiid;
  public $apikey;
  public $apisecret;
  public $apiurl;
  
  public function __construct() {
    $this->pfid = all::$cfg['gamebook_pfid'];
    $this->apiid = all::$cfg['gamebook_apiid'];
    $this->apikey = all::$cfg['gamebook_apikey'];
    $this->apisecret = all::$cfg['gamebook_apisecret'];
    $this->apiurl = all::$cfg['gamebook_apiurl'];
    $this->op = new Facebook( $this->apikey, $this->apisecret );
  }
  
  /**
   * 获取当前登录用户的平台ID.字符串型,没有获取到则为空字符串
   */
  public function getUserId() {
    Facebook::asFunP($this->apikey, $this->apisecret);
    //$this->op->require_login();
   
   echo $uid = $this->op->user;
    return $uid;
  }
  
  public function getUserInfo( $uid ) {
    if( !strlen( $uid ) ) {
      return array();
    }
    $uinfo = $this->op->api_client->users_getInfo($uid, 'uid, last_name, profile_url, pic_square, sex');
    
   // $uinfo = $uinfo['user']; //二维数组
    
    return ( strcmp($uid, $uinfo['uid']) !== 0) ? array() : array( //错误则返回空数组,否则严格返回以下格式
                                  'puid' => $uinfo['uid'],
                                  'uname' => $uinfo['last_name'],
                                  'headurl' => $uinfo['profile_url'],
                                  'sex' => $uinfo['sex'],
                                  'psuid' => '',
                                  'upage' => 'http://gamebook.tw/profile.do?id=' . $uinfo['uid'],
                                  'usid' => 0, //用户的子ID,用于同城或者本站
                                  'pfid' => all::$cfg['gamebook_pfid'],
                                  );
  }
  
  public function getAllFriends(){
    $list = $this->op->api_client->friends_list;
    return is_array($list['uid']) ? $list['uid'] : array();
  }
  
  public function getAppFriends(){
    $list = $this->op->api_client->added;
    return is_array($list['uid']) ? $list['uid'] : array();
  }
/**
   * 应用授权，用户是否添加此应用或已登录
   */
  public function isAdded() {
  	$ok=Facebook::asFunP($this->apikey, $this->apisecret);
  	//print_r($ok);
    //$this->op->require_login();
    //$this->op->require_install();
  }
  
  public function getSiteInfo() {
    
  }
  public function sendNotice() {
    
  }
  public function sendFeed( $aFields ) {
    
  }
  public function setProfile() {
    
  }
  public function setKey( $key ) {
    $this->op->session_key = $key;
  }
  public function getpayurl() {
    return '';
  }
}